package com.example.client;

import com.pdool.api.constants.MsgDefine;
import com.pdool.api.msg.LoginReq;
import org.java_websocket.client.WebSocketClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.nio.ByteBuffer;

@SpringBootApplication
public class ClientApplication implements CommandLineRunner {
	@Autowired
	private WebSocketClient pdoolWebSocketClient;
	public static void main(String[] args) {
		SpringApplication.run(ClientApplication.class, args);

	}

	@Override
	public void run(String... args) throws Exception {
		LoginReq.Builder builder = LoginReq.newBuilder();
		builder.setName("香菜");
		builder.setId(100);

		byte[] byteArray = builder.build().toByteArray();
		ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + byteArray.length);
		buffer.putInt(MsgDefine.LOGIN);
		buffer.put(byteArray);

		pdoolWebSocketClient.send(buffer.array());
	}
}
